﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ArtificialIntelligence;
using PDS2;
using FarseerPhysics.Factories;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;

namespace PwnDatSkeleton2
{
    public class Enemy : UsingAI
    {
        Player player;

        public int Health = 100;

        public int Damage = 5;
        DamageDisplay dmgDisplay;

        bool isStrut;

        public Rectangle Rectangle;

        public bool IsAlive = true;
        bool isStunned;

        float radius;

        /// <summary>
        /// Enemy mob AI
        /// </summary>
        /// <param name="screenSize">Area the mob navigates on</param>
        /// <param name="random"></param>
        /// <param name="player"></param>
        /// <param name="world"></param>
        public Enemy(Vector2 screenSize, Random random, Player player, World world)
            : base(random, screenSize)
        {
            radius = 0.5f;

            SetupBody(world, radius);
            
            isStrut = true;
            Position = new Vector2(random.Next(0, (int)screenSize.X), random.Next(0, (int)screenSize.Y)) / 64;
            
            velocity = 100;

            this.player = player;
        }

        void SetupBody(World world, float radius)
        {
            body = BodyFactory.CreateCircle(world, radius, 1f);
            body.BodyType = BodyType.Dynamic;
            body.CollidesWith = Category.Cat1 | Category.Cat3 | Category.Cat4;
            body.CollisionCategories = Category.Cat2;
            body.UserData = this;
            body.FixedRotation = true;
            body.OnCollision += new OnCollisionEventHandler(body_OnCollision);
            body.LinearDamping = 8;
        }

        private bool body_OnCollision(Fixture a, Fixture b, Contact contact)
        {
            if (b.Body == player.Body)
            {
                player.TakeDamage(body.Position *64, Damage);
            }
            return true;
        }

        /// <summary>
        /// Rabbit AI
        /// </summary>
        /// <param name="player"></param>
        /// <param name="aiMode"></param>
        /// <param name="pos"></param>
        /// <param name="world"></param>
        public Enemy(Player player, AIMode aiMode, Vector2 pos, World world)
            : base(aiMode)
        {
            radius = 0.2f;
            SetupBody(world, radius);
            this.player = player;
            this.Position = pos;
            velocity = 50;
            this.aiMode = aiMode;
        }

        float stunTimer;

        GameTime gameTime;
        public void UpdateGameTime(GameTime gameTime)
        {
            this.gameTime = gameTime;
        }
        public override void Draw(SpriteBatch batch)
        {
            if (!isStunned)
            {
                #region EnemyStrut
                if (isStrut)
                {
                    if (Vector2.Distance(player.Position, Position) <= 300)
                    {
                        aiMode = AIMode.Follow;
                    }
                    else
                    {
                        aiMode = AIMode.Strut;
                    }

                    if (Vector2.Distance(player.Position, Position) <= 300 && Vector2.Distance(player.Position, Position) >= 1)
                    {
                        Vector2 direction = Vector2.Normalize(player.Body.Position - Position);
                        body.LinearVelocity = direction / 64 * velocity;
                    }
                }
                #endregion


                Update(player.Position);
            }
            else
            {
                stunTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (stunTimer > 0.5f)
                {
                     isStunned = false;
                     stunTimer = 0;
                }
            }
            if (Position.Y > player.Position.Y)
            {
                depth = 0.1f;
            }
            else
                depth = 0.02f;
            base.Draw(batch);
        }

        public void IsDamaged(int dmg)
        {
            dmgDisplay.AddDisplay(dmg.ToString(), Position - new Vector2(Texture.Width/2,Texture.Height/2 + 20), Color.DarkRed);
            Vector2 direction = Vector2.Normalize(Position - player.Position);
            body.LinearVelocity = direction/64 * 120;

            Health -= dmg;
            if (Health < 1)
            {
                Die();
                IsAlive = false;
            }
            isStunned = true;
        }

        void Die()
        {
            body.Dispose();
        }

        public void LoadDmgDisplay(DamageDisplay dmg)
        {
            this.dmgDisplay = dmg;
        }

        public override void LoadContent(ContentManager content, string texName)
        {
            base.LoadContent(content, texName);
        }

        public override void Update(Vector2 position)
        {
            Rectangle = new Rectangle((int)Position.X - Texture.Width/2, (int)Position.Y - Texture.Height/2, Texture.Width, Texture.Height);
            if(!isStunned)
                base.Update(position);
        }
    }
}
